<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class survey extends a_base {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_class');
    }

    public function index() {
        $post = $this->input->post();
        $where = array();
        if($post != false){
            foreach($post as $key=>$val){
                if($val != ''){
                    if($key == 'isrecommend' || $key == 'isclose'){
                        if($val != '-1'){
                            $where['su.'.$key] = $val;
                        }
                    }else{
                        $where['su.'.$key] = $val;
                    }
                }
            }
        }

        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $condition = array(
            'from' => 'survey as su',
            'where' => $where,
        );
        $cnt = $this->m_public->get_list_count($condition);
        $cnt = $cnt['cnt'];
        $page_size = 20;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);
        $condition1 = array(
            'from' => 'survey as su',
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'join' => array(
                'admin as admin' => 'su.adminid=admin.adminid',
                'class as class' => 'su.classid=class.classid'
            ),
            'column' => 'su.*,admin.adminname,class.classname'
        );
        $data['list'] = $this->m_public->get_list($condition1);
        if(!empty($where)){
            $data['filter'] = 1;
        }else{
            $data['filter'] = 0;
        }

        $this->load->view('survey/index', $data);
    }

    public function survey_add($surveryid){
        $data['config'] = $this->m_public->getAllInfo('config');
        if($surveryid){
            $data['info'] = $this->m_public->getRowInfo('survey',array('surveyid'=>$surveryid));
            $class_info = $this->m_public->getRowInfo('class',array('classid'=>$data['info']['classid']),'configid');
            $data['info']['configid'] = $class_info['configid'];
            $cl_data = array(
                'configid' => $data['info']['configid'],
            );
            $data['classtree'] = $this->m_class->get_class_tree($cl_data);
        }else{
            $data['info'] = array();
        }
        $this->load->view('survey/survey_add',$data);
    }

    public function survey_action($surveryid){
        $post = $this->input->post();
        if($surveryid){
            if(!isset($post['isrecommend'])){
                $post['isrecommend'] = 0;
            }
            $this->m_public->editInfo(array('surveyid'=>$surveryid),$post,'survey');
        }else{
            $post['adminid'] = $this->session->userdata['user_id'];
            $post['addtime'] = date('Y-m-d H:i:s',time());
            $this->m_public->addRowInfo('survey',$post);
        }
        redirect('survey/index');
    }

    public function delete_info($url,$parmykey,$table,$ids){
        if(!$ids){
            redirect("survey/$url");
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $res = $this->m_public->delInfo($table,array($parmykey=>$v));
        }
        redirect("survey/$url");
    }

    public function edit_close($url,$parmykey,$table,$val,$ids){
        if(!$ids){
            redirect(b_url("survey/$url"));
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $data['isclose'] = $val;
            $this->m_public->editInfo(array($parmykey=>$v),$data,$table);
        }
        redirect("survey/$url");
    }

    function move($ids = '') {
        $post = $this->input->post();
        if($post != false){
            $idArr = explode('.', $ids);
            $info['classid'] = $post['classid'];
            foreach($idArr as $val){
                $this->m_public->editInfo(array('surveyid'=>$val),$info,'survey');
            }
            redirect('survey/index');
        }else{
            $data['config'] = $this->m_public->getAllInfo('config');
            $data['ids'] = $ids;
            $this->load->view('survey/move', $data);
        }
    }

    function get_class() {
        $configid = $this->input->get('configid');
        $data = array(
            'configid' => $configid,
        );
        $list = $this->m_class->get_class_tree($data);
        $html = '<option value="">请选择</option>';
        foreach($list as $key=>$val){
            $html .= '<option value="'.$key.'">'.$val.'</option>';
        }
        echo $html;
        /*echo json_encode($list);*/
    }

    public function surveyitem($surveyid){
        $data['info'] = $this->m_public->getRowInfo('survey',array('surveyid'=>$surveyid));
        $post = $this->input->post();
        $where = array('suitem.surveyid'=>$surveyid);
        if($post != false){
            foreach($post as $key=>$val){
                if($val != ''){
                    if($key == 'isclose' || $key == 'typeid'){
                        if($val != '-1'){
                            $where['suitem.'.$key] = $val;
                        }
                    }else{
                        $where['suitem.'.$key] = $val;
                    }
                }
            }
        }

        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $condition = array(
            'from' => 'surveyitem as suitem',
            'where' => $where,
        );
        $cnt = $this->m_public->get_list_count($condition);
        $cnt = $cnt['cnt'];
        $page_size = 20;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);
        $condition1 = array(
            'from' => 'surveyitem as suitem',
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'join' => array(
                'admin as admin' => 'suitem.adminid=admin.adminid',
            ),
            'column' => 'suitem.*,admin.adminname'
        );
        $data['list'] = $this->m_public->get_list($condition1);
        if(!empty($where)){
            $data['filter'] = 1;
        }else{
            $data['filter'] = 0;
        }

        $this->load->view('survey/surveyitem',$data);
    }

    public function surveyitem_add_new($surveyid){
        $data['info']['surveyid'] = $surveyid;
        $this->load->view('survey/surveyitem_add_new',$data);
    }

    public function surveyitem_new_action($surveyid){
        $post = $this->input->post();
        $data['itemname'] = $post['itemname'];
        $data['typeid'] = $post['typeid'];
        $data['surveyid'] = $surveyid;
        $data['listid'] = $post['listid'];
        $data['adminid'] = $this->session->userdata['user_id'];
        $data['addtime'] = date('Y-m-d H:i:s',time());
        $data['isclose'] = $post['isclose'];
        $this->m_public->addRowInfo('surveyitem',$data);

        $max_info = $this->m_public->getRowInfo('surveyitem',array(),'max(surveyitemid) as mid');

        if($post['typeid'] != 3){
            foreach($post['itemoption']['name'] as $key=>$val){
                $data1['itemoptionname'] = $val;
                $data1['listid'] = $post['itemoption']['listid'][$key];
                $data1['surveyitemid'] = $max_info['mid'];
                $data1['adminid'] = $this->session->userdata['user_id'];
                $this->m_public->addRowInfo('surveyitemoption',$data1);
            }
        }

        redirect('survey/surveyitem/'.$surveyid);
    }

    public function surveyitem_add($surveyitemid){
        if($surveyitemid){
            $data['info'] = $this->m_public->getRowInfo('surveyitem',array('surveyitemid'=>$surveyitemid));
            $data['option'] = $this->m_public->getAllInfo('surveyitemoption',array('surveyitemid'=>$surveyitemid));
        }else{
            $data['info'] = array();
        }
        $this->load->view('survey/surveyitem_add',$data);
    }

    public function surveyitem_action($surveyitemid){
        $post = $this->input->post();
        if($surveyitemid){
            $this->m_public->delInfo('surveyitemoption',array('surveyitemid'=>$surveyitemid));
            $data['itemname'] = $post['itemname'];
            $data['typeid'] = $post['typeid'];
            $data['listid'] = $post['listid'];
            $this->m_public->editInfo(array('surveyitemid'=>$surveyitemid),$data,'surveyitem');
        }
        if($post['typeid'] != 3){
            foreach($post['itemoption']['name'] as $key=>$val){
                $data1['itemoptionname'] = $val;
                $data1['listid'] = $post['itemoption']['listid'][$key];
                $data1['surveyitemid'] = $surveyitemid;
                $data1['adminid'] = $this->session->userdata['user_id'];
                $this->m_public->addRowInfo('surveyitemoption',$data1);
            }
        }
        redirect('survey/surveyitem/'.$surveyitemid);
    }

    public function delete_item($surveyid,$ids){
        if(!$ids){
            redirect("survey/surveyitem/".$surveyid);
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $this->m_public->delInfo('surveyitemoption',array('surveyitemid'=>$v));
            $res = $this->m_public->delInfo('surveyitem',array('surveyitemid'=>$v));
        }
        redirect("survey/surveyitem/".$surveyid);
    }

    public function edit_close_item($surveyid,$isclose,$ids){
        if(!$ids){
            redirect(b_url("survey/surveyitem/".$surveyid));
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $data['isclose'] = $isclose;
            $this->m_public->editInfo(array('surveyitemid'=>$v),$data,'surveyitem');
        }
        redirect("survey/surveyitem/".$surveyid);
    }

    public function surveyresult($surveyid){
        $data['info'] = $this->m_public->getRowInfo('survey',array('surveyid'=>$surveyid));
        $where = array('surveyid'=>$surveyid);
        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $condition = array(
            'from' => 'surveyresult as sures',
            'where' => $where,
        );
        $cnt = $this->m_public->get_list_count($condition);
        $cnt = $cnt['cnt'];
        $page_size = 20;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);
        $condition1 = array(
            'from' => 'surveyresult as sures',
            'limit' => $page_size,
            'offset' => $start_page,
            'where' => $where,
        );
        $data['list'] = $this->m_public->get_list($condition1);

        $this->load->view('survey/surveyresult',$data);
    }

    public function delete_result($surveyid,$ids){
        if(!$ids){
            redirect("survey/surveyresult/".$surveyid);
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $this->m_public->delInfo('surveyresult',array('surveyresultid'=>$v));
        }
        redirect("survey/surveyresult/".$surveyid);
    }

    public function surveyresult_add($surveyresultid){
        $data['info'] = $this->m_public->getRowInfo('surveyresult',array('surveyresultid'=>$surveyresultid));
        $this->load->view('survey/surveyresult_add',$data);
    }

}
